package org.zerta.troops.xml;
import org.zerta.troops.common.Game;
import org.zerta.troops.common.TurnData;

import java.io.IOException;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class TurnDataProcessor {

	public TurnData processTurnDataInput( Game game, String turnDataString ) {
		
		TurnDataUnmarshaler tdu = new TurnDataUnmarshaler(game);
		TurnData td;
		
		DOMParser parser = new DOMParser();
		try {
			parser.parse(new InputSource(new java.io.StringReader(turnDataString)));
		} catch (IOException e) {
			System.err.println("IO-fail when parsing turnDataString");
			e.printStackTrace(System.err);
			return null;
		} catch (SAXException e ) {
			System.err.println("SAX-fail when parsing turnDataString");
			return null;
		}
		
		Document doc = parser.getDocument();
		
		td = tdu.unmarshalTurnData( doc.getDocumentElement() );
		
		return td;
		
	}

	
	
}
